package com.student.service.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.student.common.utils.SecurityUtils;
import com.student.service.domain.UserClockStatus;
import com.student.service.mapper.ClockInfoMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.student.common.annotation.Log;
import com.student.common.core.controller.BaseController;
import com.student.common.core.domain.AjaxResult;
import com.student.common.enums.BusinessType;
import com.student.service.domain.ClockInfo;
import com.student.service.service.IClockInfoService;
import com.student.common.utils.poi.ExcelUtil;
import com.student.common.core.page.TableDataInfo;

/**
 * 打卡明细表Controller
 *
 * @author wanghui
 * @date 2022-08-29
 */
@RestController
@RequestMapping("/service/clockInfo")
public class ClockInfoController extends BaseController {
    @Autowired
    private IClockInfoService clockInfoService;
    @Autowired
    private ClockInfoMapper clockInfoMapper;

    /**
     * 查询打卡明细表列表
     */
    @PreAuthorize("@ss.hasPermi('service:clockInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ClockInfo clockInfo) {
        startPage();
        List<ClockInfo> list = clockInfoService.selectClockInfoList(clockInfo);
        return getDataTable(list);
    }

    /**
     * 导出打卡明细表列表
     */
    @PreAuthorize("@ss.hasPermi('service:clockInfo:export')")
    @Log(title = "打卡明细表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ClockInfo clockInfo) {
        List<ClockInfo> list = clockInfoService.selectClockInfoList(clockInfo);
        ExcelUtil<ClockInfo> util = new ExcelUtil<ClockInfo>(ClockInfo.class);
        util.exportExcel(response, list, "打卡明细表数据");
    }

    /**
     * 获取打卡明细表详细信息
     */
    @PreAuthorize("@ss.hasPermi('service:clockInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(clockInfoService.selectClockInfoById(id));
    }

    /**
     * 新增打卡明细表
     */
    @PreAuthorize("@ss.hasPermi('service:clockInfo:add')")
    @Log(title = "打卡明细表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClockInfo clockInfo) {
        return toAjax(clockInfoService.insertClockInfo(clockInfo));
    }
//    @PreAuthorize("@ss.hasPermi('service:clockInfo:add')")
    @Log(title = "打卡", businessType = BusinessType.INSERT)
    @GetMapping("/toclock")
    public AjaxResult toClock() {
        Date date=new Date();
        ClockInfo clockInfo=new ClockInfo();
        clockInfo.setStuId(SecurityUtils.getUsername());
        clockInfo.setClockTime(date);
        clockInfo.setClockStatus("1");
        Integer rows=clockInfoService.insertClockInfo(clockInfo);
        if (rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 修改打卡明细表
     */
    @PreAuthorize("@ss.hasPermi('service:clockInfo:edit')")
    @Log(title = "打卡明细表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ClockInfo clockInfo) {
        return toAjax(clockInfoService.updateClockInfo(clockInfo));
    }

    /**
     * 删除打卡明细表
     */
    @PreAuthorize("@ss.hasPermi('service:clockInfo:remove')")
    @Log(title = "打卡明细表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(clockInfoService.deleteClockInfoByIds(ids));
    }

    /**
     * 删除打卡明细表
     */
    @GetMapping("/judge")
    public AjaxResult judgeIfClock() {
        String stuId = SecurityUtils.getUsername();
       Integer num = clockInfoService.judgeIfRepeat(stuId);
        if (num>0){
            UserClockStatus userClockStatus=new UserClockStatus();
            userClockStatus.setMinecode("200");
            userClockStatus.setMsg("今日已打卡");
            return AjaxResult.success(userClockStatus);
        }else {
            UserClockStatus userClockStatus=new UserClockStatus();
            userClockStatus.setMinecode("500");
            userClockStatus.setMsg("今日未打卡");
            return AjaxResult.success(userClockStatus);
        }

    }
}
